package com.chixing.controller;

import com.chixing.pojo.Customer;
import com.chixing.pojo.NotesReview;
import com.chixing.pojo.TravelNotes;
import com.chixing.service.CustomerService;
import com.chixing.service.NotesReviewService;
import com.chixing.service.TravelNotesService;
import com.chixing.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
@RequestMapping("travelnote")
public class TravelNotesController {
    @Autowired
    private TravelNotesService travelNotesService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private NotesReviewService notesReviewService;

    //根据游记id 查询游记详情
  @GetMapping("customer_notes/{notesId}")
    public ModelAndView getById(@PathVariable("notesId") Integer notesId){
        ModelAndView mav=new ModelAndView();
        ServerResponse serverResponse=travelNotesService.getById(notesId);
        mav.addObject("serverResponse",serverResponse);
        mav.setViewName("notes/travel_notes");
        System.out.println(serverResponse);
        return mav;

    }


    @PostMapping("notes/write_notes")
    @ResponseBody
    public ModelAndView  save(@RequestParam String content,
                        @RequestParam String title,
                        @RequestParam Integer custId,
                        @RequestParam String describe,
                        @RequestParam Integer days,
                        @RequestParam String date,
                        @RequestParam String loc,
                        @RequestParam String mainImgUrl){
        ServerResponse response = travelNotesService.save(content, title, custId, describe, days, date, loc, mainImgUrl);
        ServerResponse customerServiceById = customerService.getById(custId);
        Customer customer = (Customer) customerServiceById.getData();
        ModelAndView mav = new ModelAndView();
        mav.addObject("cust",customer);
        mav.addObject("response",response);
        mav.addObject("text",content);
        mav.setViewName("notes/travel_notes");
        return mav;
    }
    //根据游记id 删除游记

    @DeleteMapping("customer/customer_notes/{notesId}")
    @ResponseBody
    public ServerResponse delete(@PathVariable("notesId") Integer notesId){
        return travelNotesService.delete(notesId);
  }


    //根据用户id，查询全部游记
    /*@GetMapping("customer/customer_notes/{custId}")
    @ResponseBody
    public ServerResponse getAllByCustId(@PathVariable("custId") Integer custId){
        return  travelNotesService.getAllByCustId(custId);
    }*/

    //分页查询
    /*@GetMapping({"customer/customer_notes/{custId}","customer/customer_notes/{custId}/page/{pageNum}"})
    @ResponseBody
    public ServerResponse getByPage(@PathVariable(value = "pageNum",required = false) Integer pageNum) {
        if (pageNum == null)
            pageNum = 1;
        return travelNotesService.getByPage(pageNum);
    }*/



    //根据用户id 分页查询所有游记
    @GetMapping(value = {"customer/customer_notes/{custId}","customer/customer_notes/{custId}/page/{pageNum}"})
    @ResponseBody
    public ServerResponse getByCustIdPage(@PathVariable("custId") Integer custId,
                                          @PathVariable(value = "pageNum",required = false) Integer pageNum){
        if (pageNum==null){
            pageNum=1;
        }
        return travelNotesService.getByCustIdPage(custId, pageNum);
    }

    @PostMapping("review/go/{notesId}")
    @ResponseBody
    public ServerResponse  insertReviewByCustId(@PathVariable("notesId")Integer notesId,
                                       @RequestParam Integer custId,
                                       @RequestParam String text){

        ServerResponse insert = notesReviewService.insert(notesId, custId, text);
        return insert;
    }

}
